﻿#region [ License information          ]
/* ************************************************************
 *
 * Copyright (c) Bjørn Bouet Smith, 2010, 2011
 *
 * This source code is subject to terms and conditions of 
 * Microsoft Public License (Ms-PL).
 * 
 * A copy of the license can be found in the license.txt
 * file at the root of this distribution. If you can not 
 * locate the License, please send an email to bjornsmith@gmail.com
 * 
 * By using this source code in any fashion, you are 
 * agreeing to be bound by the terms of the Microsoft 
 * Permissive License.
 *
 * You must not remove this notice, or any other, from this
 * software.
 *
 * ************************************************************/
#endregion


using System;
using System.Collections.Generic;
using System.Net;

namespace Smith.AMC.Configuration
{
    /// <summary>
    /// Class that parses the configuration into objects the memcached client can use
    /// </summary>
    public class MemcachedConfigurationParser
    {
        /// <summary>
        /// The configuration this parser is returning data from
        /// </summary>
        private readonly MemcachedClientConfiguration configuration;

        /// <summary>
        /// Initializes a new instance of the <see cref="MemcachedConfigurationParser"/> class.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        public MemcachedConfigurationParser(MemcachedClientConfiguration configuration)
        {
            this.configuration = configuration;
        }

        /// <summary>
        /// Gets the memcached server list that was in the configuration
        /// </summary>
        public List<KeyValuePair<IPAddress, int>> MemcachedServerList
        {
            get
            {
                List<KeyValuePair<IPAddress, int>> servers = new List<KeyValuePair<IPAddress, int>>();

                foreach (MemcachedServerNodeConfiguration elem in configuration.MemcachedNodes)
                {
                    IPAddress ipAddress;
                    if (!IPAddress.TryParse(elem.HostName, out ipAddress))
                    {
                        IPAddress[] hostAddresses = Dns.GetHostAddresses(elem.HostName);
                        if (hostAddresses.Length == 0)
                        {
                            // Not a valid ip address and we cannot look up hostname in dns
                            continue;
                        }

                        // Pick first ip address
                        ipAddress = hostAddresses[0];
                    }

                    servers.Add(new KeyValuePair<IPAddress, int>(ipAddress, elem.TcpPortNumber));
                }

                return servers;
            }
        }

        /// <summary>
        /// Gets the max sockets per server.
        /// </summary>
        public int MaxSocketsPerServer
        {
            get
            {
                return configuration.NumberOfSockets;
            }
        }

        /// <summary>
        /// Gets the socket timeout that was configured in the memcached configuration
        /// </summary>
        public TimeSpan SocketTimeout
        {
            get
            {
                return TimeSpan.FromSeconds(configuration.ConnectionTimeout);
            }
        }
    }
}
